<?php include_once "config/config.php"; session_start(); if (isset($_REQUEST['_SESSION'])) die("Get lost Muppet!");
?>
<div>
    <span class="h2">Mano pasirinktos paslaugos</span>
    <div class="separator">&nbsp;</div>
    <table border="0" class="lentele" width="90%" cellspacing="0">
        <tr>
            <td colspan="2" class="theader" style="border-right:solid 1px; padding-right: 7px;"><span>Pavadinimas</span></td>
            <td class="theader" style="border-right:solid 1px; padding-left: 7px; padding-right: 7px;"><span>Galioja nuo<span></td>
            <td class="theader" style="padding-left: 7px;"><span>Galioja iki<span></td>
        </tr>
        <tr>
            <?php
                $id = $_SESSION['stalas'];
                $vartotojas = mysql_query("SELECT Vardas, Pavarde, Password, PasswordQuestion, PasswordAnswer, email, Telefonas, Adresas FROM vartotojai WHERE id='$id'");
                $result = mysql_query("SELECT id, Planas, GaliojaNuo, GaliojaIki FROM sutartys WHERE VartotojaiId = '$id'");
                $paslaugos = mysql_query("SELECT paslaugos.id, pavadinimas FROM vartotojai INNER JOIN sutartys ON vartotojai.id = sutartys.VartotojaiId
                                         INNER JOIN paslaugos ON sutartys.Planas = paslaugos.id WHERE vartotojai.id = '$id'");
                
                $sutartys = array();
                $paslauga = array();
                $i = 0;             
                while ($row = mysql_fetch_array($paslaugos)) {
                    $paslauga[$row['id']] = $row['pavadinimas'];
                }
                while ($row = mysql_fetch_array($result)){
                    $date1 = $row['GaliojaNuo'];
                    $data1 = is_string($date1) ? strtotime($date1) : (is_int($date1) ? $date1 : time());
                    $y1 = date('Y-m-d', $data1);
                    $date2 = $row['GaliojaIki'];
                    $data2 = is_string($date2) ? strtotime($date2) : (is_int($date2) ? $date1 : time());
                    $y2 = date('Y-m-d', $data2);                    
                    
                    $sutartys[$i]['id'] = $row['id'];
                    $sutartys[$i]['planas'] = $paslauga[$row['Planas']];
                    $sutartys[$i]['nuo'] = $y1;
                    $sutartys[$i]['iki'] = $y2;
                    $i++;
                }
                for ($i = 0; $i < count($sutartys); $i++) {
                    echo('<td style="padding: 0 7px 0 0;"><span>'.$sutartys[$i]['planas'].'</td>');
                    echo('<td style="border-right:solid 1px;"><img src="img/document_icon.png" alt="doc" style="border:none; cursor:pointer;" id="doc'.$sutartys[$i]['id'].'" /></td>');
                    echo('<td style="border-right:solid 1px; padding: 0 7px;">'.$sutartys[$i]['nuo'].'</span></td>');
                    echo('<td style="padding: 0 0 0 7px;">'.$sutartys[$i]['iki'].'</td></tr>');
                }
                echo('</table></td>');
            ?>
        </tr>
    </table>
</div>